﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MTQ.Common
{
    public class FileHelper
    {

        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="sPath">    年月  例  2011-04</param>
        /// <param name="sFileName">月日  例  04-22</param>
        /// <param name="content">时间+  内容</param>
        /// <returns></returns>
        public static bool WriteFile(string sPath, string sFileName, string content)
        {
            try
            {
                StreamWriter sr;
                if (!Directory.Exists(sPath))
                {
                    Directory.CreateDirectory(sPath);
                }
                string v_filename = sPath + "\\" + sFileName;


                if (!File.Exists(v_filename)) //如果文件存在,则创建File.AppendText对象
                {
                    sr = File.CreateText(v_filename);
                    sr.Close();
                }
                using (FileStream fs = new FileStream(v_filename, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Write))
                {
                    using (sr = new StreamWriter(fs, Encoding.GetEncoding("GB2312")))
                    {

                        sr.WriteLine(content);
                        sr.Close();
                    }
                    fs.Close();
                }
                return true;

            }
            catch { return false; }
        }


        /// <summary>
        /// 将CSV文件的数据读取到DataTable中
        /// </summary>
        /// <param name="fileName">CSV文件路径</param>
        /// <returns>返回读取了CSV数据的DataTable</returns>
        public static DataTable OpenCSV(string filePath)
        {
            //Encoding encoding = Common.GetType(filePath); //Encoding.ASCII;//
            DataTable dt = new DataTable();
            FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

            //StreamReader sr = new StreamReader(fs, Encoding.UTF8);
            StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("GB2312"));
            //string fileContent = sr.ReadToEnd();
            //encoding = sr.CurrentEncoding;
            //记录每次读取的一行记录
            string strLine = "";
            //记录每行记录中的各字段内容
            string[] aryLine = null;
            string[] tableHead = null;
            //标示列数
            int columnCount = 0;
            //标示是否是读取的第一行
            bool IsFirst = true;
            //逐行读取CSV中的数据
            while ((strLine = sr.ReadLine()) != null)
            {
                //strLine = Common.ConvertStringUTF8(strLine, encoding);
                //strLine = Common.ConvertStringUTF8(strLine);

                if (IsFirst == true)
                {
                    tableHead = strLine.Split(',');
                    IsFirst = false;
                    columnCount = tableHead.Length;
                    //创建列
                    for (int i = 0; i < columnCount; i++)
                    {
                        DataColumn dc = new DataColumn(i.ToString());
                        dt.Columns.Add(dc);
                    }
                }

                aryLine = strLine.Split(',');
                DataRow dr = dt.NewRow();
                for (int j = 0; j < columnCount; j++)
                {
                    dr[j] = aryLine[j];
                }
                dt.Rows.Add(dr);

            }


            sr.Close();
            fs.Close();
            return dt;
        }


        /// <summary>
        /// 写txt文件
        /// </summary>
        /// <param name="content"></param>
        /// <param name="logpath"></param>
        public static void WriteMessage( string logpath, string content)
        {
            DateTime dt = System.DateTime.Now;
            using (FileStream fs = new FileStream(logpath, FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Write))
            {
                using (StreamWriter sr = new StreamWriter(fs, Encoding.GetEncoding("GB2312")))
                {
                    sr.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss:fff   ") + content);
                    sr.Close();
                }
                fs.Close();
            }
        }

        /// <summary>
        /// 清楚文件
        /// </summary>
        /// <param name="logpath"></param>
        public static void ClearMessage(string logpath)
        {
            DateTime dt = System.DateTime.Now;
            using (FileStream fs = new FileStream(logpath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.Write))
            {
                fs.Seek(0, SeekOrigin.Begin);
                fs.SetLength(0);
                fs.Close();
            }
        }

        /// <summary>
        /// 读取日志
        /// </summary>
        /// <param name="logpath"></param>
        /// <returns></returns>
        public static string ReadMessage(string logpath)
        {
            int i = 0;
            string str = "";
            while (i++ < 100)
            {
                try
                {
                    StreamReader sr = new StreamReader(logpath, Encoding.Default);
                    str = sr.ReadToEnd();
                    sr.Close();
                    break;
                }
                catch
                {
                    Thread.Sleep(100);
                }
            }
            return str;

        }


    }

}
